#include "stdafx.h"
#include "correctness_test.h"
#include <thread>

//---------------------------------------------------------------------------
correctness_test::correctness_test(void)
{
	BOOL is_wow64 = FALSE;
	::IsWow64Process(::GetCurrentProcess(), &is_wow64);

	SYSTEM_INFO si = { 0 };

	if(is_wow64)
	{
		printf("in WOW64\r\n");
		::GetNativeSystemInfo(&si);
	}
	else
	{
		::GetSystemInfo(&si);
	}
}
//---------------------------------------------------------------------------
void correctness_test::go(void)
{
	std::thread t1(static_threads_func, this);
	std::thread t2(static_threads_func, this);

	t1.join();
	t2.join();
}
//---------------------------------------------------------------------------
void correctness_test::static_threads_func(correctness_test* instance)
{
	instance->threads_func();
}
//---------------------------------------------------------------------------
void correctness_test::threads_func()
{
	_ring.register_to_ring();

	{
		std::unique_lock<std::mutex> ul(_lock);
		if(_sleeping_thread_id == 0){
			_sleeping_thread_id = ::GetCurrentThreadId();
			printf("%d - waiting thread\r\n", _sleeping_thread_id);
		}
	}

	DWORD last_thread = 0;

	for(int i=0 ; i<_loops ; i++)
	{
		_ring([&]()->void
		{
			// this code is fair!
			if(_sleeping_thread_id == ::GetCurrentThreadId())
			{
				::Sleep(1000);
			}

			printf("%d\r\n", ::GetCurrentThreadId());
		});
	}
}
//---------------------------------------------------------------------------